package metricsdata;

import java.util.ArrayList;



public class IntegerArray2ValueMetric extends AbstractMetricAttribute {

	private ArrayList<ArrayList<Integer>> value;
	
	public IntegerArray2ValueMetric(ArrayList<ArrayList<Integer>> value, String shortcut, String fullName, String description) {
		super(shortcut,fullName,description);
		
		this.value = value;
	}

	/**
	 * Constructor for String representations of value.
	 * String representation:
	 * [[],[1,5],[-1,0,null],null]
	 * 
	 * @param value
	 * @param idOfMetricNamesInterface
	 */
	public IntegerArray2ValueMetric(String value,  String shortcut, String fullName, String description) {
		super(shortcut,fullName,description);
		
		if(value==null || value.equals("null")) return;
		
		//check format
		
		//regular expression
		//positive or negative number: "-?\\d+"
		// + or null: ((-?\\d+)|null)
		// + in [..] and from second element separated by ",": (\\[((-?\\d+)|null)(,(-?\\d+)|,null)*\\])
		// or just an empty []: (\\[\\])
		//innerArrayRepresentation
		String iAR = "(\\[((-?\\d+)|null)(,(-?\\d+)|,null)*\\])|(\\[\\])";
		//outerArrayRepresentation
		String oAR = "\\[(("+iAR+")|null)(,("+iAR+")|,null)*\\]|\\["+iAR+"]";
		
		//System.out.println(oAR);
		
		if( ! value.matches(oAR)){
			System.err.println("IntegerArray2ValueMetric: wrong format: '"+value+"' for "+shortcut);
			return;
		}
		
		this.value = new ArrayList<ArrayList<Integer>>();
		
		//strip [ and ] from value
		String temp = value.substring(1, value.length()-1);
		
		//split by ","
		//[[1,2],null,[1,2,3],[]]
		
		temp=temp.replaceAll("\\],\\[", "];[");
		temp=temp.replaceAll("\\],null", "];null");
		temp=temp.replaceAll("null,\\[", "null;[");
		//System.out.println(temp);
		String[] split = temp.split(";");
		//System.out.println(split);
		
		//iterate over items and add them to the array list
		for(String item : split){
			
			if(item.equals("null")){
				this.value.add(null);
			}
			else{
				ArrayList<Integer> innerList = new ArrayList<Integer>();
				
				//empty list
				if(item.equals("[]")){
					this.value.add(innerList);
					continue;
				}
				
				//strip [ and ]
				temp = item.substring(1, item.length()-1);
				
				//split by ,
				String[] split2 = temp.split(",");
				
				//iterate over inner lists
				for(String item2 : split2){
					if(item2.equals("null")){
						innerList.add(null);
					}
					else{
						try{
							innerList.add(new Integer(item2));
						}
						catch(NumberFormatException e){
							System.err.println("IntegerArray2ValueMetric: could not parse number given as String: '"+item2+"'  for "+shortcut +" " +value);
							continue;
						}
					}
				}
				this.value.add(innerList);
				
			}
			
			
		}
	}
	
	@Override
	/**
	 * Converts data into a String:
	 * [[],[0,5],[-1,10,null],null]
	 */
	public String toDatabaseString() {
		if(value==null){
			return "null";
		}
		
		String result = "[";
		boolean addComma = false;
		for(ArrayList<Integer> item : value){
			//add no comma for the first element
			if(addComma){
				result += ",";
			}
			else{
				addComma = true;
			}
			
			if(item == null){
				result += "null";
			}
			else{
				boolean addComma2 = false;
				result+="[";
				for(Integer item2 : item){
					
					if( ! addComma2){
						result += item2;
						addComma2 = true;
					}
					else{
						result += ","+item2;
						
					}
				}
				result+="]";
			}
			
			
		}
		result += "]";
		return result;
	}

	@Override
	public ArrayList<ArrayList<Integer>> getValue(){
		return value;
	}
	
	@Override
	//TODO: implement
	public Double getAvgValue() {
		
		int itemCount = 0;
		Integer sum = null;
		
		if(value==null){
			return null;
		}
		
		for(ArrayList<Integer> innerList : value){
			if(innerList!=null){
				for(Integer item : innerList){
					if(item!=null){
						if(sum!=null){
							sum += item;
							itemCount++;
						}
						else{
							sum = new Integer(item);
						}
					}
				}
			}
		}
		return new Double(sum)/itemCount;
	}

	@Override
	//TODO: implement
	public Integer getFirstValue() {
		return 0;
//		if(value != null && value.size()!=0){
//			return value.get(0);
//		}
//		else{
//			return null;
//		}
	}

	@Override
	//TODO: implement
	public Integer getItemCount() {
		return 0;
//		if(value != null){
//			return value.size();
//		}
//		else{
//			return null;
//		}
	}

	@Override
	//TODO: implement
	public Integer getMaxValue() {
		return 0;
//		if(value != null){
//			Integer max = null;
//			for(Integer d : value){
//				if(max == null || d > max){
//					max = d;
//				}
//			}
//			return max;
//		}else{
//			return null;
//		}
	}

	@Override
	/**
	 * 
	 */
	public Integer getSumValue() {
		Integer sum = null;
		
		if(value==null){
			return null;
		}
		
		for(ArrayList<Integer> innerList : value){
			if(innerList!=null){
				for(Integer item : innerList){
					if(item!=null){
						if(sum!=null){
							sum += item;
						}
						else{
							sum = new Integer(item);
						}
					}
				}
			}
		}
		return sum;
	}

	@Override
	//TODO: implement
	public Integer getMinValue() {
		return 0;
//		if(value != null){
//			Integer min = null;
//			for(Integer d : value){
//				if(min == null || d < min){
//					min = d;
//				}
//			}
//			return min;
//		}else{
//			return null;
//		}
	}

}
